package com.hh.game.service.gateway.codec.websocket;

import com.alibaba.fastjson.JSONObject;
import com.hh.game.common.base.manager.session.Session;
import com.hh.game.service.gateway.codec.Request;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

public class WebSocketRequest implements Request {
    private InputMessage inputMessage;
    private Session session;
    private ChannelHandlerContext channelHandlerContext;

    private boolean isByte;
    private boolean isString;
    private byte[] bytes;
    private String stringDate;
    private JSONObject data;

    public WebSocketRequest(InputMessage inputMessage, Session session, ChannelHandlerContext channelHandlerContext) {
        this.inputMessage = inputMessage;
        this.session = session;
        this.channelHandlerContext = channelHandlerContext;

        this.bytes = this.inputMessage.getBytes();
        this.isByte = this.inputMessage.isByte();
        this.stringDate = this.inputMessage.getStringData();
        this.isString = this.inputMessage.isString();
        this.data = this.inputMessage.getData();
    }

    @Override
    public Object getParams() {
        return isByte ? bytes : (isString ? stringDate : data);
    }

    @Override
    public Session session() {
        return session;
    }

    @Override
    public String getRoute() {
        return inputMessage == null ? null : inputMessage.getRoute();
    }

    @Override
    public Channel channel() {
        return channelHandlerContext.channel();
    }

    public InputMessage getInputMessage() {
        return inputMessage;
    }

    public ChannelHandlerContext getChannelHandlerContext() {
        return channelHandlerContext;
    }

    public byte[] getBytes() {
        return bytes;
    }

    public JSONObject getData() {
        return data;
    }

    public boolean isByte() {
        return isByte;
    }
}
